// 并行网关节点
import {GraphModel, h, LogicFlow, Polygon, PolygonNode, PolygonNodeModel} from "@logicflow/core";
import {genBpmnId, groupRule} from "@logicflow/extension/src/bpmn-elements/utils";
import {parallelIcon} from "../icon"
import {FlowNodeTypeEnum} from "@/enums/FlowNodeTypeEnum"
import NodeConfig = LogicFlow.NodeConfig;

class MyPolygonNode extends PolygonNode {
    getShape() {
        const {model} = this.props
        const {x, y, width, height, points} = model as PolygonNodeModel
        const style = model.getNodeStyle()
        return h(
            'g',
            {
                transform: `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`,
            },
            h(Polygon, {
                ...style,
                x,
                y,
                points,
            }),
            h('path', {
                fill: '#6692CC',
                d: parallelIcon,
            }),
        )
    }
}

class MyPolygonNodeModel extends PolygonNodeModel {
    constructor(data: NodeConfig, graphModel: GraphModel) {
        if (!data.id) {
            data.id = `p_gateway_${genBpmnId()}`
        }
        if (!data.text) {
            data.text = ''
        }
        if (data.text && typeof data.text === 'string') {
            data.text = {
                value: data.text,
                x: data.x,
                y: data.y + 40,
            }
        }
        super(data, graphModel)
        this.points = [
            [25, 0],
            [50, 25],
            [25, 50],
            [0, 25],
        ]
        groupRule.call(this)
    }
}

// 注册组件
export function register(lf: LogicFlow) {
    lf.register({
        type: FlowNodeTypeEnum.PARALLEL_GATEWAY,
        view: MyPolygonNode,
        model: MyPolygonNodeModel,
    })
}